package com.demo.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/demo01.do")
public class Demo01Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过Servlet的 resp向前端输出页面
        //防止响应乱码
        resp.setContentType("text/html;charset=UTF-8");
        //1.获取到响应对象的字符输出流
        PrintWriter writer = resp.getWriter();
        //2.向浏览器进行字符的输出
        writer.write("<!DOCTYPE html>");
        writer.write("<html lang=\"en\">");
        writer.write("<head>");
        writer.write("    <meta charset=\"UTF-8\">");
        writer.write("    <title>样例页面</title>");
        writer.write("</head>");
        writer.write("<body>");
        writer.write("<h1>标题1</h1>");
        //实现动态拼接
        writer.write("<h1>" + new Date() + "</h1>");
        writer.write("<h2>标题2</h2>");
        writer.write("<h3>标题3</h3>");
        writer.write("</body>");
        writer.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
